
$(function(){
    var obj={
        type_ids:{},
        type_deviceId_sort:{0:{},1:{},2:{}},
        init:function(){
            // debugger;
            this.initResultTemplate();
            this.initData();
            this.eventInt();

        },
        initResultTemplate:function(){ //初始化结果模板
            var _this=this;
            var re = new RegExp(",","g");
            var gasStr= "#"+_gasType.replace(re,",#");

            $("#jdjgId").load(_root+"/template/loadCertResult "+gasStr,function () {
                _this.initResultData();
            });


        },
        initResultData:function(){
            for(var key in _keyValue){
                $("#hjwdId").text(_keyValue[key].hjwd);
                $("#hjsdId").text(_keyValue[key].hjsd);
                if(key=="kh"){
                    $("#extContentId").html(
                    "<p>校准结果不确定度：</p>"+
                    "<div style='padding-left: 150px;'>压力（20Mpa） U=0.1 MPa  k=2<br />"+
                    "压力（10Mpa）U=0.2 MPa  k=2<br />"+
                    "压力（5Mpa） U=0.3 MPa  k=2<br /></div>"
                    );
                }
                certCalculation(key,_keyValue[key],true);
            }
        },
        initData:function(){
            var _this=this;
            if(_rs!=""){
                //
                $.each(_rs,function (index,obj) {
                    var deviceType=obj.device_type;
                    var deviceId=obj.device_id_;
                    var sort=obj.sort;
                    if(_this.type_ids[deviceType]==null){
                        _this.type_ids[deviceType]=deviceId;
                    }else{
                        _this.type_ids[deviceType]=_this.type_ids[deviceType]+","+deviceId;
                    }
                    // if(_this.type_deviceId_sort[deviceType]==null){
                    //     _this.type_deviceId_sort[deviceType]={}
                    // }
                    _this.type_deviceId_sort[deviceType][deviceId]=sort;
                })
            }

            //初始化标准物质
            var ids0=_this.type_ids[0];
            if(ids0!=null && ids0!=""){
                this.getSelectedBzwz(ids0);
            }

            //初始计量标准装置
            var ids1=_this.type_ids[1];
            if(ids1!=null && ids1!=""){
                this.getSelectedJlbzzz(ids1);
            }







        },
        eventInt:function(){
            var _this=this;

            //保存证书内容
            $("#saveCertId").click(function(){
                var content= $("#contentId").prop("outerHTML");
                var orderId=$("#orderId").val();
                var equId=$("#equId").val();

                $.ajax({
                    cache: false,
                    type: "POST",
                    headers: headers,
                    url:_root+'/template/saveCert',
                    data:{"content":content,"orderId":orderId,"equId":equId},
                    async: false,
                    dataType:"text",
                    error: function(request) {
                        // pp.$.messager.alert('操作提示',"操作失败！","error");
                    },
                    success: function(data) {
                        if(data && "success" == data){
                            alert("保存成功");
                        }else{
                            alert("保存失败");
                        }
                    }
                });

            });




        },
        getSelectedBzwz:function(ids){
            var _this=this;
            $.ajax({
                type: 'POST',
                headers: headers,
                url: _root+"/standardMeasureScope/selectBzwz" ,
                data:{"standardMatterIds":ids},
                dataType: "json",
                success: function (data) {
                    // $("#bzwzTbodyId").html("");
                    var bzwzTbodyIdStr=$("#bzwzjlsbId").html();
                    for(var i=0;i<data.length;i++){
                        bzwzTbodyIdStr+="<tr>";
                        bzwzTbodyIdStr+="<td>"+data[i]["matter_name"]+"</td>"
                        bzwzTbodyIdStr+="<td>"+data[i]["measure_scope"]+"<img width='100px' height='20px' src='"+_root+"/measureunitpng/"+data[i]["measure_unit"]+"' /></td>"
                        bzwzTbodyIdStr+="<td>"+data[i]["memo"]+"</td>"
                        bzwzTbodyIdStr+="<td>"+data[i]["cert_code"]+"</td>"
                        bzwzTbodyIdStr+="<td>"+data[i]["end_date"]+"</td>"
                        bzwzTbodyIdStr+="</tr>";
                    }
                    $("#bzwzjlsbId").html(bzwzTbodyIdStr);


                    //初始计量标准装置
                    var ids2=_this.type_ids[2];
                    if(ids2!=null && ids2!=""){
                        _this.getSelectedJlzz(ids2);
                    }
                },
                error:function(){

                }

            });
        },
        getSelectedJlbzzz:function(ids){
            var _this=this;
            $.ajax({
                type: 'POST',
                headers: headers,
                url: _root+"/standardMeasureScope/selectJlbzzz" ,
                data:{"deviceIds":ids},
                dataType: "json",
                success: function (data) {
                    // $("#bzwzTbodyId").html("");
                    var tbodyIdStr=$("#jlbzzzId").html();
                    for(var i=0;i<data.length;i++){
                        tbodyIdStr+="<tr>";
                        tbodyIdStr+="<td>"+data[i]["device_name"]+"</td>"
                        tbodyIdStr+="<td>"+data[i]["measure_scope"]+"</td>"
                        tbodyIdStr+="<td>"+data[i]["memo"]+"</td>"
                        tbodyIdStr+="<td>"+data[i]["cert_code"]+"</td>"
                        tbodyIdStr+="<td>"+data[i]["end_date"]+"</td>"

                        tbodyIdStr+="</tr>";
                    }
                    $("#jlbzzzId").html(tbodyIdStr);
                },
                error:function(){

                }

            });
        },
        getSelectedJlzz:function(ids){
            var _this=this;
            $.ajax({
                type: 'POST',
                headers: headers,
                url: _root+"/standardMeasureScope/selectJlzz" ,
                data:{"equIds":ids},
                dataType: "json",
                success: function (data) {
                    // $("#bzwzTbodyId").html("");
                    var tbodyIdStr=$("#jlzzTbodyId").html();
                    for(var i=0;i<data.length;i++){
                        tbodyIdStr+="<tr>";
                        tbodyIdStr+="<td>"+data[i]["equ_name"]+"</td>"
                        tbodyIdStr+="<td>"+data[i]["measure_scope"]+"</td>"
                        tbodyIdStr+="<td>"+data[i]["equ_memo"]+"</td>"
                        tbodyIdStr+="<td>"+data[i]["cert_code"]+"</td>"
                        tbodyIdStr+="<td>"+data[i]["next_equ_date"]+"</td>"
                        tbodyIdStr+="</tr>";
                    }
                    $("#jlzzTbodyId").html(tbodyIdStr);
                },
                error:function(){

                }

            });
        }
    }
    obj.init();
})